
// la seguente funzione permette di creare lheader e le righe di una tabella html dato un risultato strutturato json
function jsonToTable(jsondata){
	var headCreated = false;
    var tbl_head = "";
    var tbl_body = "";
	var tbl_header = "";
	$.each(jsondata, function(k1,obj) {
		var tbl_row = "";
        $.each(obj, function(k2 , model) {
	        $.each(model, function(field , value) {
        		if (!headCreated)
		        	tbl_header += "<th>"+field+"</th>";
	            tbl_row += "<td>"+value+"</td>";
	        })
        })
		headCreated = true;
        tbl_body += "<tr>"+tbl_row+"</tr>";
    })
	tbl_head += "<thead><tr>" + tbl_header + "</tr></thead>";
	return tbl_head+tbl_body;
}

function dateAddDays(date,days) {
	var ms = 24 * 60 * 60 * 1000 * days;
	return new Date(date.getTime()+ms);
}

function setFlash(message,level,options) {
	//se esiste già un messaggio lo tolgo..
	//if(humane!=null && humane!=undefined)humane.remove();
	if (options==null) options={};
	if (level==null) level = "info";
	
	if (level=="warning"){
	 options =  { timeout: 100000, clickToClose: true, addnCls: 'humane-warning', waitForMove : false };	
	 message = "<i class='icon-exclamation-sign' style='margin-left:7px;'></i> "+message;			  
	}
	if (level=="error") {
		options =  { timeout: 100000, clickToClose: true, addnCls: 'humane-error', waitForMove : false};		
		message = "<i class='icon-remove' style='margin-left:7px;'></i> "+message;			  
	}
	//$("#message").html("<div class='alert alert-" + level +"'><button type='button' class='close' data-dismiss='alert'>&times;</button><p class='text-cente'>"+message+"</p></div>");
	humane.log(message,options);
}

function stopFlash() {
 	 humane.remove();
}


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


function handleFileSelect(evt) {
	 if (window.File && window.FileReader && window.FileList && window.Blob) {
    
	    var files = evt.target.files; // FileList object
	    var img = arguments[1];
	    var absoluteUrl = arguments[2];
	
	    // Loop through the FileList and render image files as thumbnails.
	    for (var i = 0, f; f = files[i]; i++) {
	
	      // Only process image files.
	      if (!f.type.match('image.*')) {
	      	setFlash("Alcuni allegati non sono immagini e nn saranno caricati.");
	        continue;
	      }
	      img.addClass("loading-image");
          img.removeClass("swing animated");

	      // var reader = new FileReader();
	      // // Closure to capture the file information.
	      // reader.onload = (function(theFile) {
	        // return function(e) {
	          // img.css("background","url("+e.target.result+") center center no-repeat");
	          // img.removeClass("loading-image");
	          // img.addClass("swing animated");
	        // };
	      // })(f);
	
	       var xhr = new XMLHttpRequest();
	        xhr.onreadystatechange = function(ev){
	            //document.getElementById('filesInfo').innerHTML = 'Done!';
			    //reader.readAsDataURL(f);
	            if (xhr.readyState==4 && xhr.status==200){
    			      responseJson = JSON.parse(xhr.responseText);
    			      if (responseJson.success) {
    			      	  path_foto = absoluteUrl+responseJson.fileuploaded[0]["thumb_webpath"];
    			      	  console.log(path_foto);
				      	  img.css("background","url("+path_foto+") center center no-repeat");
				          img.removeClass("loading-image");
				          img.addClass("swing animated");
    			      } else {
    			      	setFlash(responseJson.message,responseJson.level);
    			      }
	            	
	            }
	        };
	        xhr.open('POST', absoluteUrl+'UploadUser/ajaxupload', true);
	        var data = new FormData();
	        data.append('file', f);
	        data.append('attachTipo', 'FOTO');
	        xhr.send(data);
	    }
    }else{
    	 alert('The File APIs are not fully supported in this browser.');
    }
  }


$(document).ready(function() {
	$("[data-confirm]").each(function(){
			$(this).click(function(event) {
				if(!confirm("Sei sicuro di voler eseguire l'operazione?")) {
					event.preventDefault();
					return false;
				}
			}); 
	});
})